import Vue from 'vue';
import Router from 'vue-router';
import Home from './views/Home.vue';
import store from "./store";
import Utils from '@/assets/js/utils'


Vue.use(Router);

const router = new Router({
  // base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
    },
    {
      path: '/thank',
      name: 'thank',
      component: () => import(/* webpackChunkName: "survey" */ './views/Thank.vue'),
    },
    {
      path: '/over',
      name: 'over',
      component: () => import(/* webpackChunkName: "survey" */ './views/Over.vue'),
    },
    {
      path: '/mobile',
      name: 'mobile',
      component: () => import(/* webpackChunkName: "survey" */ './views/Mobile.vue'),
    },
  ],
});
router.beforeEach((to, from, next) => {

  let isOver = Utils.isOver()
  console.log(isOver)
  setTimeout(() => {
    const storage = JSON.parse(localStorage.getItem("survey-storage"));
    console.log(storage)
    console.log(to.name)
    if (isOver && to.name == 'home') {
      next('/over')
    } else if (storage) {
      store.commit("syncState", { storage });
      if (!storage.hadListen && to.name == 'home') {
        next('/thank')
      } else if (storage.mobile && to.name != 'thank') {
        next('/thank')
      } else {
        next();
      }
    }
    // else if (!storage && to.name == 'thank') {
    //   next('/')
    // }
    else {
      next();
    }
  }, 10)
});

export default router
